package com.qianda.qdminiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianda.qdminiapp.common.constant.CommonConstant;
import com.qianda.qdminiapp.config.CompanyProperties;
import com.qianda.qdminiapp.domain.data.QqdStoreData;
import com.qianda.qdminiapp.domain.dto.PageRequest;
import com.qianda.qdminiapp.domain.model.device.QqdStore;
import com.qianda.qdminiapp.dao.device.QqdStoreDao;
import com.qianda.qdminiapp.service.QqdStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cw
 * @since 2019-08-14
 */
@Service
@EnableConfigurationProperties({ CompanyProperties.class })
public class QqdStoreServiceImpl extends ServiceImpl<QqdStoreDao, QqdStore> implements QqdStoreService {

    @Autowired
    private QqdStoreDao qqdStoreDao;

    @Autowired
    private CompanyProperties companyProperties;

    @Override
    public IPage<QqdStoreData> findNearStore(String storeName, Double lat, Double lng, PageRequest pageRequest) {

        IPage<QqdStoreData> qqdStoreIPage = new Page<>(pageRequest.getPage(),pageRequest.getSize());
        List<QqdStoreData> store = qqdStoreDao.findStore(qqdStoreIPage, lng, lat, companyProperties.getId(), storeName);
        for(QqdStoreData k:store){
            if(k.getDistance() != null){
                if(  k.getDistance().compareTo(BigDecimal.valueOf(1000))>0){
                    k.setDistanceString( k.getDistance().multiply(new BigDecimal("0.001")).setScale(2,BigDecimal.ROUND_HALF_UP).toString()+"km");
                }else{
                    k.setDistanceString(k.getDistance().toString()+"m");
                }
            }
        }

        qqdStoreIPage.setRecords(store);

        return qqdStoreIPage;
    }

    @Override
    public List<QqdStore> findNearStoreByLatAndLng(Double lat, Double lng) {
        //根据当前经纬度查询附近门店，默认5公里范围
        List<QqdStore> qqdStores = qqdStoreDao.findStoreByLngAndLat(lng, lat, 5000.00);
        return qqdStores;
    }
}
